﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DGSFramework;
using System.Xml.Linq;
using System.Web.UI.HtmlControls;

namespace Framework
{
    public partial class Articles : System.Web.UI.Page
    {
        public DGSFrameworkEntities entity = new DGSFrameworkEntities();

        public List<udv_Categories> allCate = new List<udv_Categories>();
        public List<udv_Articles> allArt = new List<udv_Articles>();
        public List<udv_Products> allPro = new List<udv_Products>();

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                LoadCacheCategory();
                LoadCacheArticle();
                LoadArticles();
            }
        }
        public void LoadCacheCategory()
        {
            if (Cache["Category"] == null)
            {
                Cache["Category"] = allCate = entity.udv_Categories.ToList();
            }
            else
            {
                allCate = (List<udv_Categories>)Cache["Category"];
            }
        }

        public void LoadCacheArticle()
        {
            if (Cache["Article"] == null)
            {
                Cache["Article"] = allArt = entity.udv_Articles.Include("udv_Categories").ToList();
            }
            else
            {
                allArt = (List<udv_Articles>)Cache["Article"];
            }
        }
        protected void LoadArticles()
        {
            try
            {
                int cateID = Int32.Parse(Request.QueryString["CateID"]) ;
                var cateCurrent = allCate.Where(ca => ca.CateID == cateID).FirstOrDefault();
                var lstallArt = allArt.Where(a => a.udv_Categories.CateID == cateID).OrderBy(a => a.DisplayOrder).ThenByDescending(a => a.AddedDate).ToList();
                if (lstallArt.Count == 1)
                {
                    Response.Redirect(ResolveUrl("~/" + Globals.GetUrl(lstallArt.FirstOrDefault().UrlRewrite, Globals.GetUrlAspx(lstallArt.FirstOrDefault().UrlRewrite, false) + lstallArt.FirstOrDefault().ArticleID)));
                }
                rptArticle.DataSource = lstallArt;
                rptArticle.DataBind();
                Controls_PagingControl pagingControl = (Controls_PagingControl)PagingControl1;
                LoadPaging(lstallArt, ref pagingControl, ref rptArticle, cateCurrent);
            }
            catch (Exception)
            {

                throw;
            }
        }       
        public void LoadPaging(List<udv_Articles> allArticles, ref Controls_PagingControl PagingControl, ref Repeater rpControl, udv_Categories cate)
        {
            double NOProducts = 0;

            PagingControl.pageUrl = ResolveUrl("~/" + DGSFramework.Globals.GetUrl(cate.UrlRewrite, DGSFramework.Globals.GetUrlAspx(cate.UrlRewrite, true) + cate.CateID));

            object objPage = Request.QueryString["page"];
            // check paging
            if (objPage != null)
            {
                int pageindex;
                if (int.TryParse(objPage.ToString(), out pageindex))
                {
                    PagingControl.PageIndex = pageindex;
                }
            }
            else
                PagingControl.PageIndex = 1;

            // check exp must exist
            if (allArticles.Count() == 0)
            {
                return;
            }
            // cal paging
            NOProducts = (double)allArticles.Count() / cate.PagingSize.Value;
            char splitChar = '.';

            if (NOProducts.ToString().Contains('.'))
            { NOProducts++; splitChar = '.'; }

            if (NOProducts.ToString().Contains(','))
            { NOProducts++; splitChar = ','; }

            PagingControl.TotalPage = int.Parse(NOProducts.ToString().Split(splitChar).GetValue(0).ToString());

            if (PagingControl.TotalPage <= 1)
                PagingControl.Visible = false;
            else
                PagingControl.Visible = true;

            allArticles = allArticles.Count() <= cate.PagingSize.Value ? allArticles : allArticles
                .Skip((PagingControl.PageIndex - 1) * cate.PagingSize.Value).Take(cate.PagingSize.Value).ToList();
            rpControl.DataSource = allArticles;
            rpControl.DataBind();
        }

    }
}